﻿/*******************************************************************************
 **  Name:                                        **
 **  Description:                                                             **
 **                                                                           **
 **  CDDL: Open Source Initiative (OSI) Approved License                            **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  The contents of this file are subject to the terms of the CDDL:          **
 **  Common Development and Distribution License (the "License").             **
 **  You may not use this file except in compliance with the License.         **
 **                                                                           **
 **  You can obtain a copy of the license at $PROJECT_ROOT/LICENSE            **
 **  or http://www.opensolaris.org/os/licensing.  This code is Open Source    **
 **  and you are free to use it within the confines of the license, even      **
 **  for your own closed-source commercial projects, as long as you follow    **
 **  the terms set forth in the CDDL.                                         **
 **                                                                           **
 **  When distributing Covered Code, include this CDDL HEADER in each         **
 **  file and include the License file at $PROJECT_ROOT/LICENSE.              **
 **  If applicable, add the following below this CDDL HEADER, with the        **
 **  fields enclosed by brackets "[]" replaced with your own identifying      **
 **  information: Portions Copyright [yyyy] [name of copyright owner]         **
 **                                                                           **
 ******************************************************************************/
// Created on November 1, 2010, 7:25 AM
// Modified $Date$ by $Author$
// $Id$

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace sat1baf1
{
    ///<summary>
    ///This is the class that contains the structure of an Agent in Spycraft 2.0
    ///</summary>
    ///<remarks>
    ///This Agent class has public members currently simply because I didn't want to write
    ///property methods for them since they'll be accessed fairly often.
    ///</remarks>
    public class Agent
    {
        public enum ATTRIBUTE { STR, DEX, CON, INT, WIS, CHA, UNKNOWN };
        //Personal
        public string Name = "UNK";     // eg Johnathan Hathaway
        public string Passwd = "UNK";   // passwd to sign on
        public string Codename = "UNK"; // sign on with this
        public string Player = "UNK";
        public string _aliases = "UNK";
        public int CurrXP = 0;
        public int Rep_Net = 0;
        public short CareerLevel = 0;
        public string Height = "UNK";
        public string Weight = "UNK";
        public short Age = 0;
        public string EyeColor = "UNK";
        public string HairColor = "UNK";
        public byte Gender = 0;
        public string Nationality = "UNK";

        // Stats
        public sbyte [] STR = { 0,0,0,0 };  // score, mod, temp score, temp mod
        public sbyte [] DEX = { 0, 0, 0, 0 };
        public sbyte [] CON = { 0, 0, 0, 0 };
        public sbyte [] INT = { 0, 0, 0, 0 };
        public sbyte [] WIS = { 0, 0, 0, 0 };
        public sbyte [] CHA = { 0, 0, 0, 0 };

        public short[] Vitality = { 0,0 };
        public short[] Wounds = { 0,0 };
        public short[] Subdual = { 0,0 };
        public short[] Stress = { 0,0 };

        // Saving throws
        public short[] Fortitude = { 0, 0, 0, 0 };
        public short[] Reflex = { 0, 0, 0, 0 };
        public short[] Will = { 0, 0, 0, 0 };

        // Base attacks
        public short[] Unarmed = { 0, 0, 0, 0 };
        public short[] Melee = { 0, 0, 0, 0 };
        public short[] Ranged = { 0, 0, 0, 0 };

        public short[] Defense = { 0, 0, 0, 0, 0, 0 };
        public short[] Initiative = { 0, 0, 0, 0 };

        // Checks
        public short[] KnowledgeCheckBonus = { 0, 0, 0, 0 };
        public short[] RequestCheckBonus = { 0, 0, 0, 0 };
        public short[] GearCheckBonus = { 0, 0, 0, 0 };

        public short[] ActionDice = { 0, 0, 0 };
      

        // Skills TODO should use a dictionary of skillcheck
        //public List<Skills> SkillList;

        // Status/Operational Level
        public byte OpLevel = 0;    // OpLevel 0-7
        public string Mission = "UNK";  // ie Operation Bluejay

        // Agency Information

        public Agent()
        {
            //SkillList = new List<Skills>();
        }
    }

    ///<summary>
    ///This is the XML-encapsulating class for our DB
    ///</summary>
    ///
    public class AgentDB
    {
        static string _fileName;
        XElement _x;

        static AgentDB _instance;
        public static AgentDB Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new AgentDB();
                return _instance;
            }
        }

        private AgentDB()
        {
            _fileName = @"Data Files\Agents.xml";
            _x = XElement.Load(_fileName); // Root element
            XmlLog.Instance.Add("Loaded AgentDB log from " + _fileName);
        }

        public Agent GetAgent(string name)
        {
            IEnumerable<XElement> wpn =
                from el in _x.Elements("NAME")
                where (string)el.Element("NAME") == name
                select el;

            Agent newAgent = new Agent();
            foreach (XElement el in wpn)
            {

            }
            return newAgent;
        }

        public IEnumerable<XElement> FindAgentByName(string name)
        {
            IEnumerable<XElement> wpn =
                from el in _x.Elements("Name")
                select el;

            return wpn;
        }
        public IEnumerable<XElement> FindAgentByCodename(string name)
        {
            IEnumerable<XElement> wpn =
                from el in _x.Elements("Agent")
                where el.Element("Codename").Value == name
                select el;
            
            return wpn;
        }
        //        public IEnumerable<XElement> FindWeaponByNode(string node)
//        {
//            // The event arg name is the name of the node
//#if DEBUG
//            XmlLog.Instance.Add("FindWeaponByNode: Looking for <NAME id=\" " + node + "\">");
            
//#endif
//            IEnumerable<XElement> wpn = 
//                from x in _x.Elements("WEAPON")
//                where (string)x.Element("NAME").Attribute("id") == node
//                select x;
            
//            return wpn;
//        }

        public void Add(XElement newAgent)
        {
            _x.Add(newAgent);
            _x.Save(_fileName);
        }

        public void Clear()
        {
            _x.RemoveNodes();
            _x.Save(_fileName);
        }


        // Properties
        // Read-only
        public XElement AllAgents { get { return _x; } }



    }
}
